﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.HCLoad.Parameters;
using YArchitech.HCLoad.Utils;

namespace YArchitech.HCLoad.Query
{
	public class PropertyGridAttritutes : List<PropertyGridAttritute>, ICustomTypeDescriptor
	{
		public PropertyGridAttritutes(SpaceAttributeFile spaceAttributeFile)
		{
			this.FillData(spaceAttributeFile);
		}

		private bool FillData(SpaceAttributeFile spaceAttributeFile)
		{
			this.m_SpaceAttributeFile = spaceAttributeFile;
			if (spaceAttributeFile == null)
			{
				return false;
			}
			foreach (SpaceAttributeGroup spaceAttributeGroup in spaceAttributeFile.Groups)
			{
				foreach (SpaceAttribute att in spaceAttributeGroup.Attributes)
				{
					base.Add(new PropertyGridAttritute(att));
				}
			}
			return true;
		}

		public bool Attach(Space space)
		{
			foreach (PropertyGridAttritute propertyGridAttritute in this)
			{
				Guid guid = new Guid(propertyGridAttritute.SpaceAttribute.GUID);
				Parameter parameter = space.get_Parameter(guid);
				if (parameter == null)
				{
					double num = 0.0;
					propertyGridAttritute.Value = num;
				}
				else
				{
					ParameterType parameterType = parameter.Definition.ParameterType;
                    if ((int)parameterType != 2)
					{
                        if ((int)parameterType != 3)
						{
							propertyGridAttritute.Value = parameter.AsValueString();
						}
						else if (HcUtils.IsEnglish)
						{
							if (parameter.Definition.Name.Contains("Humidity Load"))
							{
								if (parameter.Definition.Name.Contains("Humidity Load Density"))
								{
									propertyGridAttritute.Value = parameter.AsDouble().ToString() + " kg/(h.m²)";
								}
								else
								{
									propertyGridAttritute.Value = parameter.AsDouble().ToString() + " kg/h";
								}
							}
							else
							{
								propertyGridAttritute.Value = parameter.AsDouble();
							}
						}
						else if (parameter.Definition.Name.Contains("湿负荷"))
						{
							if (parameter.Definition.Name.Contains("湿负荷指标"))
							{
								propertyGridAttritute.Value = parameter.AsDouble().ToString() + " kg/(h.m²)";
							}
							else
							{
								propertyGridAttritute.Value = parameter.AsDouble().ToString() + " kg/h";
							}
						}
						else
						{
							propertyGridAttritute.Value = parameter.AsDouble();
						}
					}
					else
					{
						propertyGridAttritute.Value = parameter.AsInteger();
					}
				}
			}
			return true;
		}

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] filter)
		{
			PropertyDescriptor[] array = new PropertyDescriptor[base.Count];
			for (int i = 0; i < base.Count; i++)
			{
				array[i] = new PropertyGridAttrituteDescriptor(base[i], filter);
			}
			return new PropertyDescriptorCollection(array);
		}

		AttributeCollection ICustomTypeDescriptor.GetAttributes()
		{
			return TypeDescriptor.GetAttributes(this, true);
		}

		string ICustomTypeDescriptor.GetClassName()
		{
			return TypeDescriptor.GetClassName(this, true);
		}

		string ICustomTypeDescriptor.GetComponentName()
		{
			return TypeDescriptor.GetComponentName(this, true);
		}

		TypeConverter ICustomTypeDescriptor.GetConverter()
		{
			return TypeDescriptor.GetConverter(this, true);
		}

		EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
		{
			return TypeDescriptor.GetDefaultEvent(this, true);
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
		{
			return TypeDescriptor.GetEvents(this, attributes, true);
		}

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
		{
			return TypeDescriptor.GetEvents(this, true);
		}

		PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
		{
			return TypeDescriptor.GetDefaultProperty(this, true);
		}

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
		{
			return TypeDescriptor.GetProperties(this, true);
		}

		object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
		{
			return TypeDescriptor.GetEditor(this, editorBaseType, true);
		}

		object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
		{
			return this;
		}

		public override string ToString()
		{
			return new StringBuilder().ToString();
		}

		private SpaceAttributeFile m_SpaceAttributeFile;
	}
}
